﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace Northwind.NET.FE.Reports
{
    public partial class SalesByYearReport : Form
    {
        public SalesByYearReport()
        {
            InitializeComponent();
        }

        private int _yearNum;
        private Boolean _showDetails;
        private void SalesByYearReport_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;  
            //_yearNum = 1997;
            //_showDetails = true;
            refreshReport();
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(yearNumTextBox.Text)) return;
            bool ret = Int32.TryParse(yearNumTextBox.Text, out _yearNum);
            if (!ret) return;
            refreshReport();
        }

        private void setReportParameters()
        {
            _yearNum = Int32.Parse(yearNumTextBox.Text);  
            _showDetails = showDetailsCheckBox.Checked;
            
            LocalReport report = this.reportViewer1.LocalReport;  
            ReportParameterInfoCollection parametersInfo = report.GetParameters();

            ReportParameterInfo yearNumParamInfo = parametersInfo[0];
            ReportParameterInfo showDetailsParamInfo = parametersInfo[1];

            ReportParameter yearNumParameter = new ReportParameter();
            yearNumParameter.Name = "yearNum";
            yearNumParameter.Values.Add(_yearNum.ToString());
            ReportParameter showDetailsParameter = new ReportParameter();
            showDetailsParameter.Name = "doNotShowDetails";
            showDetailsParameter.Values.Add((!_showDetails).ToString());

            report.SetParameters(new ReportParameter[] 
                { yearNumParameter, showDetailsParameter});
        }

        private void refreshReport()
        {
            setReportParameters();
            this.salesByYearTableAdapter.Fill(this.simpleReportsDataSet.SalesByYear, _yearNum);
            this.reportViewer1.RefreshReport();
        }

        private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            refreshReport();
        }
    }
}
